package com.xmb.wechat.view.wechat.money;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.bean.WechatMsgBean;
import com.xmb.wechat.entity.WechatAppInfoBean;
import com.xmb.wechat.util.MoneyFormat;
import com.xmb.wechat.widget.InputDialog;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.OnClick;

public class WechatFundZhuanZhangDetailActivity extends BaseActivity {

    @BindView(R2.id.iv_small_icon)
    ImageView      ivSmallIcon;
    @BindView(R2.id.tv_state)
    TextView       tvState;
    @BindView(R2.id.tv_money)
    TextView       tvMoney;
    @BindView(R2.id.tv_tip_1)
    TextView       tvTip1;
    @BindView(R2.id.tv_tip_2)
    TextView       tvTip2;
    @BindView(R2.id.tv_send_time)
    TextView       tvSendTime;
    @BindView(R2.id.tv_recieve_time)
    TextView       tvRecieveTime;
    @BindView(R2.id.btn_confirm_recieve)
    Button         btnConfirmRecieve;
    @BindView(R2.id.iv_lqt_icon)
    ImageView      ivLqtIcon;
    @BindView(R2.id.re_linqiantong)
    RelativeLayout reLinqiantong;
    @BindView(R2.id.tv_lingqiantong_shouyilv)
    TextView       tvLingqiantongShouyilv;
    @BindView(R2.id.tv_lingqiantong_desc)
    TextView       mTvLingqiantongDesc;

    public static void start(Activity act, WechatContactBean wechatContactBean, WechatMsgBean wechatMsgBean) {
        Intent it = new Intent(act, WechatFundZhuanZhangDetailActivity.class);
        it.putExtra("wechatContactBean", wechatContactBean);
        it.putExtra("wechatMsgBean", wechatMsgBean);
        act.startActivity(it);
    }

    public WechatFundZhuanZhangDetailActivity() {
        super(R.layout.activity_fund_zhuanzhang_detail);
    }

    @SuppressLint({"SetTextI18n", "SimpleDateFormat"})
    @Override
    protected void initView() {
        setStatusBarColor(Color.parseColor("#ffffff"));
        setNavigationColor(Color.parseColor("#f7f7f7"));

        WechatContactBean receiveContactBean = (WechatContactBean) getIntent().getSerializableExtra("wechatContactBean");
        WechatMsgBean wechatMsgBean = (WechatMsgBean) getIntent().getSerializableExtra("wechatMsgBean");
        //指定数字字体：
        AssetManager mgr = getAssets();
        Typeface tf = Typeface.createFromAsset(mgr, "fonts/WeChatLqNumber.ttf");
        tvMoney.setTypeface(tf);
        tvLingqiantongShouyilv.setText("零钱通 七日年化" + WechatAppInfoBean.getWechat(this).getLingqiantongRate() + "%");
        if (receiveContactBean.getId() != -1) {//我发起的转账：
            ivSmallIcon.setBackgroundResource(wechatMsgBean.isReceiveMoney() ? R.drawable.wechat_transfer_success : R.drawable.wechat_transfer_receiving);
            tvState.setText(wechatMsgBean.isReceiveMoney() ? receiveContactBean.getName() + "已收款" : "待" + receiveContactBean.getName() + "确认收款");
            tvMoney.setText("¥" + MoneyFormat.formatMoney(wechatMsgBean.getMoney()));
            tvTip1.setText(wechatMsgBean.isReceiveMoney() ? "已存入对方零钱中" : "1天内朋友未确认，将退还给你。");
            tvTip2.setVisibility(wechatMsgBean.isReceiveMoney() ? View.GONE : View.VISIBLE);
            tvSendTime.setText("转账时间：" + TimeUtils.date2String(wechatMsgBean.getMsgTime(), new SimpleDateFormat("yyyy-MM-dd HH:mm")));
            tvRecieveTime.setVisibility(wechatMsgBean.isReceiveMoney() ? View.VISIBLE : View.GONE);
            reLinqiantong.setVisibility(View.GONE);
            if (wechatMsgBean.isReceiveMoney())
                tvRecieveTime.setText("收款时间：" + TimeUtils.date2String(wechatMsgBean.getReceiveTime(), new SimpleDateFormat("yyyy-MM-dd HH:mm")));
            btnConfirmRecieve.setVisibility(View.GONE);
        } else {//对方发起的转账：
            ivSmallIcon.setBackgroundResource(wechatMsgBean.isReceiveMoney() ? R.drawable.wechat_transfer_success : R.drawable.wechat_transfer_receiving);
            tvState.setText(wechatMsgBean.isReceiveMoney() ? "已收款" : "待确认收款");
            tvMoney.setText("¥" + MoneyFormat.formatMoney(wechatMsgBean.getMoney()));
            tvTip1.setText(wechatMsgBean.isReceiveMoney() ? "" : "1天内未确认，将退还给对方。");
            tvTip2.setText(wechatMsgBean.isReceiveMoney() ? "查看零钱" : "立即退还");
            tvSendTime.setText("转账时间：" + TimeUtils.date2String(wechatMsgBean.getMsgTime(), new SimpleDateFormat("yyyy-MM-dd HH:mm")));
            tvRecieveTime.setVisibility(wechatMsgBean.isReceiveMoney() ? View.VISIBLE : View.GONE);
            reLinqiantong.setVisibility(wechatMsgBean.isReceiveMoney() ? View.VISIBLE : View.GONE);
            if (wechatMsgBean.isReceiveMoney())
                tvRecieveTime.setText("收款时间：" + TimeUtils.date2String(wechatMsgBean.getReceiveTime(), new SimpleDateFormat("yyyy-MM-dd HH:mm")));
            btnConfirmRecieve.setVisibility(wechatMsgBean.isReceiveMoney() ? View.GONE : View.VISIBLE);
        }
    }

    @OnClick(R2.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    @OnClick(R2.id.tv_lingqiantong_shouyilv)
    public void onLingqiantongClicked() {
        InputDialog.showInputDialog(this, "编辑零钱通的收益率", WechatAppInfoBean.getWechat(this).getLingqiantongRate(), EditorInfo.TYPE_CLASS_PHONE, new InputDialog.OnInputDialogListener() {
            @Override
            public void onCancelClicked() {
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onOKClicked(Dialog dialog, String inputText) {
                try {
                    if (!TextUtils.isEmpty(inputText))
                        tvLingqiantongShouyilv.setText("零钱通 七日年化" + inputText + "%");
                    dialog.dismiss();
                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtils.showLong("输入格式错误！");
                }
            }
        });
    }

    @OnClick(R2.id.tv_lingqiantong_desc)
    public void onLingqiantongDescClicked() {
        InputDialog.showInputDialog(this, "编辑零钱通的收益描述", "转入赚收益 七日年化", EditorInfo.TYPE_CLASS_TEXT, new InputDialog.OnInputDialogListener() {
            @Override
            public void onCancelClicked() {
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onOKClicked(Dialog dialog, String inputText) {
                try {
                    if (!TextUtils.isEmpty(inputText))
                        mTvLingqiantongDesc.setText(inputText);
                    dialog.dismiss();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
